log
A Rust library providing a lightweight logging facade.
A logging facade provides a single logging API that abstracts over the actual logging implementation. Libraries can use the logging API provided by this crate, and the consumer of those libraries can choose the logging implementation that is most suitable for its use case.
Usage
In libraries
Libraries should link only to the log
crate, and use the provided macros to
log whatever information will be useful to downstream consumers:
[]
= "0.3"
extern crate log;
In executables
Executables should choose a logger implementation and initialize it early in the runtime of the program. Logger implementations will typically include a function to do this. Any log messages generated before the logger is initialized will be ignored.
The executable itself may use the log
crate to log as well.
The env_logger
crate provides a logger implementation that mirrors the
functionality of the old revision of the log
crate.
[]
= "0.3"
= "0.3"
extern crate log;
extern crate env_logger;
In tests
Tests can use the env_logger
crate to see log messages generated during that test:
[]
= "0.3"
[]
= "0.3"
extern crate log;
Assuming the module under test is called my_lib
, running the tests with the
RUST_LOG
filtering to info messages from this module looks like:
; ; ;
Note that env_logger::init()
needs to be called in each test in which you
want to enable logging. Additionally, the default behavior of tests to
run in parallel means that logging output may be interleaved with test output.
Either run tests in a single thread by specifying RUST_TEST_THREADS=1
or by
running one test by specifying its name as an argument to the test binaries as
directed by the cargo test
help docs:
; ; ;